use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Raft log entry entity
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "raft_logs")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub log_index: i64,
    pub log_term: i64,
    pub entry_type: String,          // "Normal", "Blank", "Membership"
    pub entry_data: Option<Vec<u8>>, // Serialized request data
    pub created_at: ChronoDateTimeUtc,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

/// Raft metadata entity
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "raft_meta")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub key_name: String,
    pub log_index: Option<i64>,
    pub log_term: Option<i64>,
    pub vote_for: Option<i64>,
    pub committed: Option<i64>,
    pub updated_at: ChronoDateTimeUtc,
}

pub mod raft_meta {
    use super::*;
    pub use ActiveModel as RaftMetaActiveModel;
    pub use Entity as RaftMetaEntity;
    pub use Model as RaftMeta;
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

/// Cluster state entity
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "cluster_state")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub state_data: Vec<u8>, // Serialized cluster state
    pub last_applied: Option<i64>,
    pub version: i64,
    pub updated_at: ChronoDateTimeUtc,
}

pub mod cluster_state {
    use super::*;
    pub use ActiveModel as ClusterStateActiveModel;
    pub use Entity as ClusterStateEntity;
    pub use Model as ClusterState;
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

/// Node information entity
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "nodes")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub node_id: String,
    pub address: String,
    pub status: String, // "Healthy", "Degraded", "Offline", "Removed"
    pub metadata: Json, // JSON metadata
    pub last_seen: ChronoDateTimeUtc,
    pub joined_at: ChronoDateTimeUtc,
    pub updated_at: ChronoDateTimeUtc,
}

pub mod nodes {
    use super::*;
    pub use ActiveModel as NodeActiveModel;
    pub use Entity as NodeEntity;
    pub use Model as Node;
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

/// Configuration entity
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "configurations")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub key_name: String,
    pub value: Json, // JSON value
    pub description: Option<String>,
    pub created_at: ChronoDateTimeUtc,
    pub updated_at: ChronoDateTimeUtc,
}

pub mod configurations {
    use super::*;
    pub use ActiveModel as ConfigurationActiveModel;
    pub use Entity as ConfigurationEntity;
    pub use Model as Configuration;
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
